// ============================================================================
// 事件相关类型定义
// ============================================================================

import { TaskProgress, TaskLogEntry, TaskStatus, NotificationData, AppConfig } from './index'

/** 事件基础接口 */
export interface BaseEvent {
  type: string
  timestamp: Date
  source?: string
}

/** 任务事件 */
export interface TaskEvent extends BaseEvent {
  taskId: string
}

/** 任务进度更新事件 */
export interface TaskProgressEvent extends TaskEvent {
  type: 'task:progress'
  progress: TaskProgress
}

/** 任务日志事件 */
export interface TaskLogEvent extends TaskEvent {
  type: 'task:log'
  log: TaskLogEntry
}

/** 任务状态变更事件 */
export interface TaskStatusEvent extends TaskEvent {
  type: 'task:status'
  status: TaskStatus
  previousStatus?: TaskStatus
}

/** 任务创建事件 */
export interface TaskCreatedEvent extends TaskEvent {
  type: 'task:created'
  taskType: string
}

/** 任务完成事件 */
export interface TaskCompletedEvent extends TaskEvent {
  type: 'task:completed'
  duration: number
  result: {
    success: boolean
    totalCount: number
    errorCount: number
  }
}

/** 账号事件 */
export interface AccountEvent extends BaseEvent {
  accountId: string
}

/** 账号登录事件 */
export interface AccountLoginEvent extends AccountEvent {
  type: 'account:login'
  method: 'manual' | 'auto'
}

/** 账号验证事件 */
export interface AccountValidationEvent extends AccountEvent {
  type: 'account:validation'
  isValid: boolean
  reason?: string
}

/** 账号删除事件 */
export interface AccountDeletedEvent extends AccountEvent {
  type: 'account:deleted'
  reason?: string
}

/** 系统事件 */
export interface SystemEvent extends BaseEvent {
  level: 'info' | 'warn' | 'error'
}

/** 配置变更事件 */
export interface ConfigChangedEvent extends SystemEvent {
  type: 'config:changed'
  changes: Partial<AppConfig>
  previousConfig?: Partial<AppConfig>
}

/** 错误事件 */
export interface ErrorEvent extends SystemEvent {
  type: 'error'
  error: {
    code: string
    message: string
    stack?: string
    context?: Record<string, any>
  }
}

/** 通知事件 */
export interface NotificationEvent extends BaseEvent {
  type: 'notification'
  notification: NotificationData
}

/** 数据导出事件 */
export interface DataExportEvent extends BaseEvent {
  type: 'data:export'
  taskId: string
  format: string
  filePath: string
  fileSize: number
}

/** 数据清理事件 */
export interface DataCleanupEvent extends BaseEvent {
  type: 'data:cleanup'
  deletedRecords: number
  freedSpace: number
}

/** 应用启动事件 */
export interface AppStartEvent extends SystemEvent {
  type: 'app:start'
  version: string
  platform: string
}

/** 应用关闭事件 */
export interface AppShutdownEvent extends SystemEvent {
  type: 'app:shutdown'
  reason: 'user' | 'system' | 'error'
  uptime: number
}

/** 窗口事件 */
export interface WindowEvent extends BaseEvent {
  windowId: string
}

/** 窗口创建事件 */
export interface WindowCreatedEvent extends WindowEvent {
  type: 'window:created'
  windowType: 'main' | 'login' | 'settings'
}

/** 窗口关闭事件 */
export interface WindowClosedEvent extends WindowEvent {
  type: 'window:closed'
}

/** 所有事件类型的联合 */
export type AppEvent = 
  | TaskProgressEvent
  | TaskLogEvent
  | TaskStatusEvent
  | TaskCreatedEvent
  | TaskCompletedEvent
  | AccountLoginEvent
  | AccountValidationEvent
  | AccountDeletedEvent
  | ConfigChangedEvent
  | ErrorEvent
  | NotificationEvent
  | DataExportEvent
  | DataCleanupEvent
  | AppStartEvent
  | AppShutdownEvent
  | WindowCreatedEvent
  | WindowClosedEvent

/** 事件监听器类型 */
export type AppEventListener<T extends AppEvent = AppEvent> = (event: T) => void | Promise<void>

/** 事件发射器接口 */
export interface EventEmitter {
  on<T extends AppEvent>(eventType: T['type'], listener: AppEventListener<T>): void
  off<T extends AppEvent>(eventType: T['type'], listener: AppEventListener<T>): void
  emit<T extends AppEvent>(event: T): void
  once<T extends AppEvent>(eventType: T['type'], listener: AppEventListener<T>): void
  removeAllListeners(eventType?: string): void
}

/** 事件过滤器 */
export interface EventFilter {
  types?: string[]
  sources?: string[]
  dateRange?: [Date, Date]
  level?: string[]
}

/** 事件查询选项 */
export interface EventQueryOptions {
  filter?: EventFilter
  limit?: number
  offset?: number
  orderBy?: 'timestamp' | 'type'
  orderDirection?: 'asc' | 'desc'
}
